package com.contract.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.contract.common.enums.ContractStateEnum;
import com.contract.common.enums.RegisterStateEnum;
import com.contract.common.enums.RegisterTypeEnum;
import com.contract.common.enums.UserStateEnum;
import com.contract.common.util.CommonUtil;
import com.contract.mapper.BizContractInfoMapper;
import com.contract.mapper.BizFileMapper;
import com.contract.mapper.SysRegisterMapper;
import com.contract.mapper.SysUserMapper;
import com.contract.pojo.bo.BizContractInfo;
import com.contract.pojo.bo.SysRegister;
import com.contract.pojo.bo.SysUser;
import com.contract.pojo.output.CarouselOutput;
import com.contract.pojo.output.JsonResult;
import com.contract.pojo.output.StatisticsOutput;
import com.contract.service.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 首页service
 *
 * @author liw
 * @version 1.0
 * @date 2022/6/13 11:10
 */
@Slf4j
@Service
public class HomeService extends BaseService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRegisterMapper sysRegisterMapper;

    @Autowired
    private BizContractInfoMapper bizContractInfoMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private FileService fileService;

    @Autowired
    private BizFileMapper bizFileMapper;

    /**
     * 统计
     *
     * @return data
     */
    public JsonResult<StatisticsOutput> statistics() {

        StatisticsOutput output = new StatisticsOutput();

        // 待审核人员
        LambdaQueryWrapper<SysRegister> registerQw = new LambdaQueryWrapper<>();
        registerQw.eq(SysRegister::getState, RegisterStateEnum.W.getKey());
        registerQw.eq(SysRegister::getType, RegisterTypeEnum.U.getKey());
        Integer count = sysRegisterMapper.selectCount(registerQw);
        output.setUserReview(count == null ? 0 : count);

        // 在职人员
        LambdaQueryWrapper<SysUser> userQw = new LambdaQueryWrapper<>();
        userQw.eq(SysUser::getState, UserStateEnum.O.getKey());
        count = sysUserMapper.selectCount(userQw);
        output.setUserOnJob(count == null ? 0 : count);

        // 总人员
        count = sysUserMapper.selectCount(null);
        output.setUserAll(count == null ? 0 : count);

        // 合同待发放
        userQw.clear();
        userQw.eq(SysUser::getState, UserStateEnum.N.getKey());
        count = sysUserMapper.selectCount(userQw);
        output.setContractProvide(count == null ? 0 : count);

        // 合同待签署
        LambdaQueryWrapper<BizContractInfo> contractQw = new LambdaQueryWrapper<>();
        contractQw.eq(BizContractInfo::getState, ContractStateEnum.S.getKey());
        count = bizContractInfoMapper.selectCount(contractQw);
        output.setContractSign(count == null ? 0 : count);

        // 合同待审核
        contractQw.clear();
        contractQw.eq(BizContractInfo::getState, ContractStateEnum.W.getKey());
        count = bizContractInfoMapper.selectCount(contractQw);
        output.setContractRewiew(count == null ? 0 : count);

        return JsonResult.success(output);
    }

    /**
     * 轮播图
     *
     * @return data
     */
    public JsonResult<List<CarouselOutput>> carousel() {
        List<CarouselOutput> list = new ArrayList<>();
        int pageNo = 1;
        int pageSize = 5;

        // 人员注册
        LambdaQueryWrapper<SysRegister> registerQw = new LambdaQueryWrapper<>();
        registerQw.eq(SysRegister::getState, RegisterStateEnum.W.getKey());
        registerQw.eq(SysRegister::getType, RegisterTypeEnum.U.getKey());
        registerQw.orderByDesc(SysRegister::getUpdateTime);
        Page<SysRegister> objectPage = new Page<>(pageNo, pageSize);
        objectPage.setOptimizeCountSql(false);
        Page<SysRegister> page = sysRegisterMapper.selectPage(objectPage, registerQw);
        if (!CommonUtil.isEmpty(page) && !CommonUtil.isEmpty(page.getRecords())) {
            for (SysRegister register : page.getRecords()) {
                CarouselOutput output = new CarouselOutput(register.getUserName(),
                        register.getCardFace(), register.getUpdateTime().toLocalDate(), "人员注册，待审核");
                list.add(output);
            }
        }

        // 待发放合同
        LambdaQueryWrapper<SysUser> userQw = new LambdaQueryWrapper<>();
        userQw.eq(SysUser::getState, UserStateEnum.N.getKey());
        userQw.orderByDesc(SysUser::getUpdateTime);
        Page<SysUser> userPage = new Page<>(pageNo, pageSize);
        objectPage.setOptimizeCountSql(false);
        Page<SysUser> userPageList = sysUserMapper.selectPage(userPage, userQw);
        if (!CommonUtil.isEmpty(page) && !CommonUtil.isEmpty(page.getRecords())) {
            for (SysUser user : userPageList.getRecords()) {
                CarouselOutput output = new CarouselOutput(user.getUserName(),
                        user.getCardFace(), user.getUpdateTime().toLocalDate(), "人员已审核，待发放合同");
                list.add(output);
            }
        }

        // 待入职
        userQw.clear();
        userQw.eq(SysUser::getState, UserStateEnum.W.getKey());
        userQw.orderByDesc(SysUser::getUpdateTime);
        objectPage.setOptimizeCountSql(false);
        userPageList = sysUserMapper.selectPage(userPage, userQw);
        if (!CommonUtil.isEmpty(page) && !CommonUtil.isEmpty(page.getRecords())) {
            for (SysUser user : userPageList.getRecords()) {
                CarouselOutput output = new CarouselOutput(user.getUserName(),
                        user.getCardFace(), user.getUpdateTime().toLocalDate(), "合同已经签署，待入职");
                list.add(output);
            }
        }
        return JsonResult.success(list);
    }

    /**
     * 轮播图
     *
     * @return data
     */
    public JsonResult<Map<String, Long>> chart() {

        LocalDate now = LocalDate.now();
        LocalDate pre = LocalDate.of(now.getYear() - 1, now.getMonthValue() + 1, 1);
        // 人员分组
        LambdaQueryWrapper<SysRegister> registerQw = new LambdaQueryWrapper<>();
        registerQw.ge(SysRegister::getCreateTime, pre.atStartOfDay());
        registerQw.select(SysRegister::getCreateTime);
        registerQw.eq(SysRegister::getType, RegisterTypeEnum.U.getKey());
        List<SysRegister> list = sysRegisterMapper.selectList(registerQw);
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(item -> item.getCreateTime().getYear() + "-" + item.getCreateTime().getMonthValue(),
                TreeMap::new, Collectors.counting()));

        return JsonResult.success(map);
    }

}
